const withLess = require("@zeit/next-less");
const withCss = require("@zeit/next-css");
const withSass = require("@zeit/next-sass");
const withFont = require('next-fonts');
const withPlugins = require("next-compose-plugins");
const withImage = require("next-images")
const cssLoaderGetLocalIdent = require("css-loader/lib/getLocalIdent.js");
const path = require("path");

module.exports = withPlugins([withLess, withCss, withImage,withSass], {
  webpack5: false,
  cssModules: false,
  lessLoaderOptions:{
    javascriptEnabled: true
  },
  cssLoaderOptions: {
    javascriptEnabled: true,
    camelCase: true,
    localIdentName: "[local]___[hash:base64:5]",
    getLocalIdent: (context, localIdentName, localName, options) => {
      let hz = context.resourcePath.replace(context.rootContext, "");
      if (/node_modules/.test(hz)) {
        return localName;
      } else {
        return cssLoaderGetLocalIdent(
          context,
          localIdentName,
          localName,
          options
        );
      }
    },
  },
  webpack(config) {
    if (config.externals) {
      const includes = [/antd-mobile/];
      config.externals = config.externals.map((external) => {
        if (typeof external !== "function") return external;
        return (ctx, req, cb) => {
          return includes.find((include) =>
            req.startsWith(".")
              ? include.test(path.resolve(ctx, req))
              : include.test(req)
          )
            ? cb()
            : external(ctx, req, cb);
        };
      });
    }
    config.module.rules.push({
      test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
      use: {
        loader: 'url-loader',
        options: {
          limit: 1024 * 8,
          esModule: false,
          name: '[name].[ext]'
        }
      }
    });
    console.log("config", JSON.stringify(config));
    config.resolve.alias = {
      ...config.resolve.alias,
      "@":path.resolve(__dirname),
      "@component": path.join(__dirname, "./component"),
      "@redux": path.join(__dirname, "./Redux"),
      "@tools": path.join(__dirname, "./tools"),
      "@views": path.join(__dirname, "./views"),
      "@images": path.join(__dirname, "./images"),
      "@config": path.join(__dirname, "./config")
    }
    return config;
  },
});

var a = {
  externals: [null],
  optimization: {
    noEmitOnErrors: true,
    checkWasmTypes: false,
    nodeEnv: false,
    splitChunks: false,
    minimize: false,
    minimizer: [null, null],
  },
  context: "/Users/lee/workspace/next-antd-mobile",
  node: { setImmediate: false },
  watchOptions: {
    aggregateTimeout: 5,
    ignored: ["**/.git/**", "**/node_modules/**", "**/.next/**", "**/.#*"],
  },
  output: {
    publicPath: "/_next/",
    path: "/Users/lee/workspace/next-antd-mobile/.next/server",
    filename: "[name].js",
    libraryTarget: "commonjs2",
    hotUpdateChunkFilename: "static/webpack/[id].[hash].hot-update.js",
    hotUpdateMainFilename: "static/webpack/[hash].hot-update.json",
    chunkFilename: "[name].js",
    strictModuleExceptionHandling: true,
    futureEmitAssets: false,
    webassemblyModuleFilename: "static/wasm/[modulehash].wasm",
  },
  performance: false,
  resolve: {
    extensions: [".js", ".mjs", ".jsx", ".json", ".wasm"],
    modules: ["node_modules"],
    alias: {
      next: "/Users/lee/workspace/next-antd-mobile/node_modules/next",
      "private-next-pages": "/Users/lee/workspace/next-antd-mobile/pages",
      "private-dot-next": "/Users/lee/workspace/next-antd-mobile/.next",
      "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/next-server/lib/router/utils/resolve-rewrites.js":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/next-server/lib/router/utils/resolve-rewrites-noop.js",
    },
    mainFields: ["main", "module"],
    plugins: [{ topLevelLoader: {} }],
  },
  resolveLoader: {
    alias: {
      "emit-file-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/emit-file-loader",
      "error-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/error-loader",
      "next-babel-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/next-babel-loader",
      "next-client-pages-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/next-client-pages-loader",
      "next-image-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/next-image-loader",
      "next-serverless-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/next-serverless-loader",
      "noop-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/noop-loader",
      "next-style-loader":
        "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/build/webpack/loaders/next-style-loader",
    },
    modules: ["node_modules"],
    plugins: [{ topLevelLoader: {} }],
  },
  module: {
    rules: [
      {
        test: {},
        include: ["/Users/lee/workspace/next-antd-mobile", {}, {}, {}, {}],
        use: {
          loader: "next-babel-loader",
          options: {
            configFile: "/Users/lee/workspace/next-antd-mobile/.babelrc",
            isServer: true,
            distDir: "/Users/lee/workspace/next-antd-mobile/.next",
            pagesDir: "/Users/lee/workspace/next-antd-mobile/pages",
            cwd: "/Users/lee/workspace/next-antd-mobile",
            cache: true,
            development: true,
            hasReactRefresh: false,
            hasJsxRuntime: true,
          },
        },
      },
      {
        oneOf: [
          {
            test: {},
            loader: "noop-loader",
            options: { __next_css_remove: true },
          },
          {
            test: {},
            issuer: {},
            use: {
              loader: "error-loader",
              options: {
                reason:
                  "CSS \u001b[1mcannot\u001b[22m be imported within \u001b[36mpages/_document.js\u001b[39m. Please move global styles to \u001b[36mpages/_app.js\u001b[39m.",
              },
            },
          },
          {
            sideEffects: false,
            test: {},
            issuer: {
              and: ["/Users/lee/workspace/next-antd-mobile"],
              not: [{}],
            },
            use: [
              {
                loader:
                  "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/compiled/css-loader/cjs.js",
                options: {
                  importLoaders: 1,
                  sourceMap: true,
                  esModule: false,
                  modules: {
                    exportLocalsConvention: "asIs",
                    exportOnlyLocals: true,
                    mode: "pure",
                  },
                },
              },
              {
                loader:
                  "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/compiled/postcss-loader/cjs.js",
                options: {
                  postcssOptions: { plugins: [null, null], config: false },
                  sourceMap: true,
                },
              },
            ],
          },
          {
            sideEffects: false,
            test: {},
            issuer: {
              and: ["/Users/lee/workspace/next-antd-mobile"],
              not: [{}],
            },
            use: [
              {
                loader:
                  "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/compiled/css-loader/cjs.js",
                options: {
                  importLoaders: 3,
                  sourceMap: true,
                  esModule: false,
                  modules: {
                    exportLocalsConvention: "asIs",
                    exportOnlyLocals: true,
                    mode: "pure",
                  },
                },
              },
              {
                loader:
                  "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/compiled/postcss-loader/cjs.js",
                options: {
                  postcssOptions: { plugins: [null, null], config: false },
                  sourceMap: true,
                },
              },
              {
                loader:
                  "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/compiled/resolve-url-loader/index.js",
                options: { sourceMap: true },
              },
              {
                loader:
                  "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/compiled/sass-loader/cjs.js",
                options: { sourceMap: true, sassOptions: {} },
              },
            ],
          },
          {
            test: [{}, {}],
            use: {
              loader: "error-loader",
              options: {
                reason:
                  "CSS Modules \u001b[1mcannot\u001b[22m be imported from within \u001b[1mnode_modules\u001b[22m.\nRead more: https://nextjs.org/docs/messages/css-modules-npm",
              },
            },
          },
          {
            test: [{}, {}],
            use: "/Users/lee/workspace/next-antd-mobile/node_modules/next/dist/compiled/ignore-loader/index.js",
          },
          {
            test: [{}, {}],
            issuer: { and: [{}] },
            use: {
              loader: "error-loader",
              options: {
                reason:
                  "Global CSS \u001b[1mcannot\u001b[22m be imported from within \u001b[1mnode_modules\u001b[22m.\nRead more: https://nextjs.org/docs/messages/css-npm",
              },
            },
          },
          {
            test: [{}, {}],
            use: {
              loader: "error-loader",
              options: {
                reason:
                  "Global CSS \u001b[1mcannot\u001b[22m be imported from files other than your \u001b[1mCustom <App>\u001b[22m. Please move all global CSS imports to \u001b[36mpages/_app.js\u001b[39m. Or convert the import to Component-Level CSS (CSS Modules).\nRead more: https://nextjs.org/docs/messages/css-global",
              },
            },
          },
        ],
      },
      {
        test: {},
        use: [
          {
            loader: "css-loader/locals",
            options: {
              modules: true,
              minimize: false,
              sourceMap: true,
              importLoaders: 0,
              camelCase: true,
              localIdentName: "[local]___[hash:base64:5]",
            },
          },
        ],
      },
      {
        test: {},
        use: [
          {
            loader: "css-loader/locals",
            options: {
              modules: true,
              minimize: false,
              sourceMap: true,
              importLoaders: 1,
              camelCase: true,
              localIdentName: "[local]___[hash:base64:5]",
            },
          },
          { loader: "less-loader", options: {} },
        ],
      },
    ],
    strictExportPresence: true,
  },
  plugins: [
    {},
    {
      definitions: {
        "process.env.NODE_ENV": '"development"',
        "process.browser": "false",
        "process.env.__NEXT_TRAILING_SLASH": "false",
        "process.env.__NEXT_BUILD_INDICATOR": "true",
        "process.env.__NEXT_PLUGINS": "false",
        "process.env.__NEXT_STRICT_MODE": "false",
        "process.env.__NEXT_REACT_ROOT": "false",
        "process.env.__NEXT_OPTIMIZE_FONTS": "false",
        "process.env.__NEXT_OPTIMIZE_IMAGES": "false",
        "process.env.__NEXT_OPTIMIZE_CSS": "false",
        "process.env.__NEXT_SCROLL_RESTORATION": "false",
        "process.env.__NEXT_IMAGE_OPTS":
          '{"deviceSizes":[640,750,828,1080,1200,1920,2048,3840],"imageSizes":[16,32,48,64,96,128,256,384],"path":"/_next/image","loader":"default","domains":[]}',
        "process.env.__NEXT_ROUTER_BASEPATH": '""',
        "process.env.__NEXT_HAS_REWRITES": "false",
        "process.env.__NEXT_I18N_SUPPORT": "false",
        "process.env.__NEXT_ANALYTICS_ID": '""',
        "global.GENTLY": "false",
      },
    },
    { options: { resourceRegExp: {}, contextRegExp: {} } },
    {
      prevAssets: null,
      previousOutputPathsWebpack5: {},
      currentOutputPathsWebpack5: {},
    },
    { serverless: false, dev: true },
    {
      options: {
        outputPath: "/Users/lee/workspace/next-antd-mobile/.next/server",
      },
    },
    {},
    {},
    {},
  ],
  mode: "development",
  name: "server",
  target: "node",
  bail: false,
  devtool: "eval-source-map",
};
